package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.ReadDAO;

import model.Veiling;

/**
 * Servlet implementation class refreshAuctions
 */
public class refreshAuctions extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PrintWriter out;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public refreshAuctions() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		out = response.getWriter();
		ReadDAO read = new ReadDAO();
		if(request.getParameter("action").equals("getRemaining")){
			int veilingid = Integer.parseInt(request.getParameter("veilingid"));
			Veiling veiling = read.getVeiling(veilingid);
			if(veiling != null){
				out.print(veiling.getRemaining());
			}
			return;
		}
		if(request.getParameter("action").equals("getAuctions")){
			for(Veiling v: read.getVeilingen()){
				if(!v.getExpired()){
					out.println("<div class=\"auction\">");
					out.println("<div class=\"auctionInfo\">");
					out.println("<h6>");
					out.println("<a href=\"./VeilingController?action=fullauction&veiling="+v.getId()+"\">"+v.getTitel());
					out.println("</a>");
					out.println("</h6>");
					out.println("<span class=\"date\">Nog <b>"+v.getRemaining()+" </b> te gaan.</span>");
					out.println("<p id=\"omschrijving\">"+v.getOmschrijving()+"</p>");
					out.println("<a id=\"more\" href=\"./VeilingController?action=fullauction&veiling="+v.getId()+"\"> ..more</a>");
					out.println("</div>");
					out.println("<div class=\"auctionTime\">");
					out.println("<ul>");
					out.println("<li>Highest bid</li>");
					out.println("<li><b>"+v.getHighestBid()+" &euro; </b></li>");
					out.println("</ul>");
					out.println("</div>");
					out.println("<img class=\"auctionimg\" src=\""+v.getFoto().toString()+" \">");
					out.println("</div>");
				}
			}
			out.println("gemaakt door: Jaap Manenschijn en Yvonne Nieuwerth");
			return;
		}
























	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
